$(function () {
   obj.init();
});
var obj = new function () {
    'use strict';
    return {
        form:null,
        leftList:null,
        rightList:null,
        iBPPId:null,
        init:function () {
            this.iBPPId = xhTool.getQuery('iBPPId');
            this.leftList = new xhDataGrid($('#leftList'), 'device/getPlanReceive',{iBPPId:this.iBPPId});
            this.leftList.parseToolbar().init();
            this.rightList = new xhDataGrid($('#rightList'), 'device/getTabReturn',{iBPPId:this.iBPPId});
            this.rightList.parseToolbar().init();
            this.listener();
            this.funObj().bindDgEvent();
        },
        listener:function () {
            var $this = this;

            $('#bt_list_search1').click(function () {
                $this.funObj().doSearchLeft();
            });

            $('#bt_list_search2').click(function () {
                $this.funObj().doSearchRight();
            });
        },
        funObj:function () {
            var $this = this;
            return {
                bindDgEvent:function () {
                    $this.rightList.command({
                        view:detailview,
                        detailFormatter:function(index,row){
                            var str='<div class="ddv" style="padding:5px 0;overflow: auto;">';
                            if (row['total'].length > 0) {
                                str +=
                                    '<table>' +
                                    '       <tbody>' +
                                    '       <tr><th style="text-align: center">平板型号</th><th style="text-align: center">数目</th></tr>';
                                $.each(row['total'], function (i,item) {
                                    str += '<tr><td style="width: 100px;text-align: center">'+item['name']+'</td><td style="width: 50px;text-align: right">'+item['num']+'</td></tr>';
                                });
                                str += '     </tbody>' +
                                    '</table>';
                                str += '<br>';
                            }

                            if (row['mData'].length > 0) {
                                str +=
                                    '<table>' +
                                    '       <tbody>' ;
                                if (row['iSource'] == 2) {
                                    str+= '       <tr><th style="text-align: center">名称</th><th style="text-align: center">设备类型</th><th style="text-align: center">设备号</th></tr>';
                                    $.each(row['mData'], function (i,item) {
                                        str+='    <tr><td style="width: 220px;"><div >';
                                        str+= item['sSchoolName'];
                                        if(item['sSchoolName']){
                                            str+= '(';
                                            str+= !item['sUserName'] ?  item['sReceiverName'] : item['sUserName'];
                                            str+= ')';
                                        }else  {
                                            str+= !item['sUserName'] ? item['sReceiverName'] : item['sUserName'] ;
                                        }
                                        str+='</div></td></td><td style="width: 100px;">'+item['sDevType']+'</td><td style="width: 170px;">'+item['sDevNumber']+'</td></tr>' ;
                                    });
                                } else if (row['iSource'] == 3){
                                    str+= '       <tr><th style="text-align: center">名称</th><th style="text-align: center">设备类型</th><th style="text-align: center">设备号</th><th style="text-align: center">绑定</th><th style="text-align: center">领用</th></tr>';
                                    $.each(row['mData'], function (i,item) {
                                        str+='    <tr><td style="width: 220px;"><div >';
                                        str+= item['sSchoolName'];
                                        if(item['sSchoolName']){
                                            str+= '(';
                                            str+= !item['sUserName'] ?  item['sReceiverName'] : item['sUserName'];
                                            str+= ')';
                                        }else  {
                                            str+= !item['sUserName'] ? item['sReceiverName'] : item['sUserName'] ;
                                        }
                                        str+='</div></td><td style="width: 100px;">'+item['sDevType']+'</td><td style="width: 170px;">'+item['sDevNumber']+'</td><td style="width: 40px;text-align: center"><div title="'+item['iADLId']+'">'+ (item['bBind'] ? '是':'否') +'</div></td><td style="width: 40px;text-align: center"><div title="'+item['iUTId']+'">'+ (item['bGetTab'] ? '是':'否') +'</div></td></tr>' ;

                                    });
                                } else if (row['iSource'] == 1) { //设备领用
                                    str+= '       <tr><th style="text-align: center">名称</th><th style="text-align: center">领用类型</th><th style="text-align: center">设备类型</th><th style="text-align: center">设备号</th></tr>';
                                    $.each(row['mData'], function (i,item) {
                                        if (item['iReceivePurpose'] == '自用机') {
                                            str+='    <tr><td style="width: 220px;"><div >';
                                            str+= !item['sStaffName'] ? item['sReceiverName'] : item['sStaffName'] ;
                                            str+= '</div></td><td>'+item['iReceivePurpose']+'</td><td style="width: 100px;">'+item['sDevType']+'</td><td style="width: 170px;">'+item['sDevNumber']+'</td></tr>' ;
                                        } else  if (item['iReceivePurpose'] == '学校公用机' || item['iReceivePurpose'] == '教师机' || item['iReceivePurpose'] == '备用机') {
                                            str+='    <tr><td style="width: 220px;"><div >';
                                            str+= item['sSchoolName'];
                                            if(item['sSchoolName']){
                                                str+= '(';
                                                str+= !item['sUserName'] ?  item['sReceiverName'] : item['sUserName'];
                                                str+= ')';
                                            }else  {
                                                str+= !item['sUserName'] ? item['sReceiverName'] : item['sUserName'] ;
                                            }
                                            str+= '</div></td><td>'+item['iReceivePurpose']+'</td><td style="width: 100px;">'+item['sDevType']+'</td><td style="width: 170px;">'+item['sDevNumber']+'</td></tr>' ;
                                        }
                                    });
                                }
                                str += '     </tbody>' +
                                    '</table>';
                            }

                            str+='</div>';
                            return str;
                        },
                        onExpandRow: function(index,row){
                            var ddv = $(this).datagrid('getRowDetail',index).find('div.ddv');
                            ddv.panel({
                                // height:200,
                                width:900,
                                border:false,
                                cache:false,
                                onLoad:function(){
                                    $this.rightList.command('fixDetailRowHeight',index);
                                    $this.rightList.command('fixDetailRowWidth',index);
                                }
                            });
                        }
                    });


                    $this.leftList.command({
                        view:detailview,
                        detailFormatter:function(index,row){
                            var str='<div class="ddv" style="padding:5px 0;overflow: auto;"  >' ;
                            if (row['prodDetail']) {
                                str +='  <div style="margin-bottom: 5px;">商品明细:</div>' +
                                    '         <table style="margin-bottom: 5px;">' +
                                    '                <tr><th style="width: 120px;text-align: center">商品名称</th><th style="width: 75px;text-align: center">商品数量</th><th style="width: 75px;text-align: center">类型</th></tr>' +
                                    '               ' + row['prodDetail'] +
                                    '         </table>';
                            }

                            if (row['tchDetail']) {
                                str += '<div style="margin-bottom: 5px;">领用详情:</div>' +
                                    '        <table style="margin-bottom: 5px;">' +
                                    '               <tr><th style="width: 75px;text-align: center">姓名</th><th style="width: 120px;text-align: center">机型</th><th style="width: 180px;text-align: center">设备号</th></tr>' +
                                    '                '+ row['tchDetail'] +
                                    '        </table>';
                            }
                            if (row['mDesc']) {
                                str += '<div style="margin-bottom: 5px;">备注:</div>' +
                                    '   <div >'+row['mDesc']+'</div>';
                            }
                            str+='</div>';
                            return str;

                        },
                        onExpandRow: function(index,row){
                            var ddv = $(this).datagrid('getRowDetail',index).find('div.ddv');
                            ddv.panel({
                                // height:200,
                                width:780,
                                border:false,
                                cache:false,
                                onLoad:function(){
                                    $this.leftList.command('fixDetailRowHeight',index);
                                    $this.leftList.command('fixDetailRowWidth',index);
                                }
                            });
                        }
                    });
                },
                doSearchLeft:function () {
                    $this.leftList.reload({
                        key:xhExtra.getValue($('#list_search1')),
                        state:xhExtra.getValue($('#select_state')),
                        type:xhExtra.getValue($('#select_type')),
                        iBPPId:$this.iBPPId
                    })
                },
                doSearchRight:function () {
                    $this.rightList.reload({
                        key:xhExtra.getValue($('#list_search2')),
                        source:xhExtra.getValue($('#select_source')),
                        states:xhExtra.getValue($('#select_states')),
                        iBPPId:$this.iBPPId
                    })
                }
            }
        }
    }
};